package web.servlets;

import domain.Category;
import domain.Item;
import domain.Product;
import persistence.Impl.TIMEtil;
import service.CatelogService;
import service.DialogService;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.util.List;
import java.util.Locale;

/**
 * @projectName: petStore
 * @package: web.servlets
 * @className: ViewProductByKeyWordServlet
 * @author: suqianbei
 * @description: TODO
 * @date: 2021/11/1 21:32
 * @version: 1.0
 */
public class ViewProductByKeywordServlet extends HttpServlet {
    private static final String VIEW_PRODUCT_KEYWORDS="/WEB-INF/jsp/catalog/Category.jsp";

    private String keywords;
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String  keyword=req.getParameter("keywords");
        keywords=keyword;
        keywords="%"+keywords.toLowerCase(Locale.ROOT)+"%";

        CatelogService catelogService =new CatelogService();
        List<Product> productList=catelogService.searchProductList(keywords);
        Category category=new Category();
        category.setName(keyword);

        HttpSession httpSession=req.getSession();
        httpSession.setAttribute("category",category);
        httpSession.setAttribute("productList",productList);
        req.getRequestDispatcher(VIEW_PRODUCT_KEYWORDS).forward(req,resp);
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doGet(req, resp);
    }
}
